<?php include('header.php');?> 
<div id="content">
	<div id="article_content">
		<?php
			function c_num_page ($count_query,$mysql_connection,$articles_num_per_page) {
				// start 返回总页数和当前页数
				$count_result = $mysql_connection->query($count_query);
				$count_record = $count_result->fetch_assoc();
				$count_record = $count_record['count'];
				$count_page = ceil($count_record/$articles_num_per_page);
				// start 如果页面数，page的值出现不正确的值，比如负值的处理
				if (isset($_GET['page']) and $_GET['page']>=0) {
					$num_page = $_GET['page'];
					$num_page = floor($num_page);
					if ($num_page > ($count_page-1)) {
						$num_page = $count_page-1;
					}
				} else {
					$num_page = 0;
				}
				// end 如果页面数，page的值出现不正确的值，比如负值的处理
				$result_f = array();
				$result_f['num_page'] = $num_page;
				$result_f['count_page'] = $count_page;
				return $result_f;
				// end 返回总页数和当前页数
			}

			// start 判断请求的页面，是分类还是存档，还是首页，然后设置变量
			if (isset($_GET['cat'])) {
				// start 如果请求的是分类
				$cat_id = $_GET['cat'];
				$count_query = "select count(*) as `count` from `articles_category` where `category_id`=".$cat_id;
				$result_f = c_num_page($count_query,$mysql_connection,$setting['articles_num_per_page']);
				$query_articles = "select * from `articles` where `id` in (select `articles_id` from `articles_category` where `category_id`=".$cat_id.") order by `post_time` desc limit ".$result_f['num_page']*($setting['articles_num_per_page']).",".$setting['articles_num_per_page'];
				// end 如果请求的是分类
			} else if (isset($_GET['m']) and isset($_GET['y'])) {
				// start 如果请求的是存档，那么设置相关变量进行查询
				$month_m = $_GET['m'];
				$year_y = $_GET['y'];
				$count_query = "select count(*) as `count` from `articles` where date_format(`post_time`,'%m') = '".$month_m."' and date_format(`post_time`,'%Y') = '".$year_y."'";
				$result_f = c_num_page($count_query,$mysql_connection,$setting['articles_num_per_page']);
				$query_articles = "select * from `articles` where date_format(`post_time`,'%m') = '".$month_m."' and date_format(`post_time`,'%Y') = '".$year_y."' order by `post_time` desc limit ".$result_f['num_page']*($setting['articles_num_per_page']).",".$setting['articles_num_per_page'];
				// end 如果请求是存档，那么设置相关变量进行查询
			} else {
				// start 如果请求的是首页
				$count_query = "select count(*) as `count` from `articles`";
				$result_f = c_num_page($count_query,$mysql_connection,$setting['articles_num_per_page']);
				$query_articles = "select * from `articles` order by `post_time` desc limit ".$result_f['num_page']*$setting['articles_num_per_page'].",".$setting['articles_num_per_page'];
				// end 如果请求的是首页
			}
			// end 判断请求的页面，是分类还是存档，还是首页，然后设置变量

			$result_articles = $mysql_connection->query($query_articles);
			$articles_num = $result_articles->num_rows;
			for ( $i=0; $i<$articles_num; $i+=1 ) {
				$row_article = $result_articles->fetch_assoc();
				// start 从数据库中取得帖子的分类信息
				$article_id = $row_article['id'];
				$query_category_id = "select * from `articles_category` where `articles_id` = ".$article_id;
				$result_category_id = $mysql_connection->query($query_category_id);
				$count_category = $result_category_id->num_rows;
				$category = array();
				for ( $j=0; $j<$count_category; $j+=1 ) {
					$each_category_id = $result_category_id->fetch_assoc();
					$query_category_name = "select * from `category` where `id`=".$each_category_id['category_id'];
					$result_category_name = $mysql_connection->query($query_category_name);
					$row_category_name = $result_category_name->fetch_assoc();
					$category[$row_category_name['id']] = $row_category_name['name'];
				}
				// end 从数据库中取得帖子的分类信息
			?>
			<!-- start 显示每个单独的帖子 -->
			<div class="article">
				<div class="article_title"><strong><a href="article.php?aid=<?php echo $row_article['id']; ?>"><?php echo $row_article['title'];?></a></strong></div>
				<div class="article_content"><?php echo $row_article['content'];?></div>
				<div class="article_footer">
					<img src="images/date_time.png"></img>
					发表时间：<?php echo $row_article['post_time'].'&nbsp;|'; ?> 
					<img src="images/comment.png"></img>
					评论个数：<a href="article.php?aid=<?php echo $row_article['id'];?>">
					<?php $num_comment = count_comment($row_article['id'], $mysql_connection); echo $num_comment;?>
					</a>&nbsp;|
					<img src="images/tag.png"></img>
					分类：<?php
						foreach ( $category as $a_category_id => $a_category_name ) {
						?>
						<a href="index.php?cat=<?php echo $a_category_id;?>"><?php echo $a_category_name.'&nbsp;';?></a>
						<?php
						}
						unset($category);
					?>
					|
				</div>
			</div>
			<!-- end 显示每个单独的帖子 -->
			<?php
			}
		?> 
		<!-- start 显示上一页下一页 -->
		<div id="next_pre_page">
			<div id="pre_page">
				<?php
					if ($result_f['num_page'] > 0) {
						if (isset($_GET['cat'])) {
						?>
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']-1); ?>&cat=<?php echo $_GET['cat'];?>"><img src="images/left.png" />&nbsp;上一页</a>
						<?php
						} else if (isset($_GET['m'])) {
						?>
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']-1); ?>&m=<?php echo $_GET['m'];?>&y=<?php echo $_GET['y'];?>"><img src="images/left.png" />&nbsp;上一页</a>
						<?php
						} else {
						?>
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']-1); ?>"><img src="images/left.png" />&nbsp;上一页</a>
						<?php
						}
					}
				?> 
			</div>
			<div id="next_page">
				<?php
					if ($result_f['num_page'] < ($result_f['count_page']-1)) {
					?>
					<?php
						if (isset($_GET['cat'])) {
						?> 
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']+1); ?>&cat=<?php echo $_GET['cat'];?>">下一页&nbsp;<img src="images/right.png" /></a>
						<?php
						} else if (isset($_GET['m'])) {
						?>
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']+1); ?>&m=<?php echo $_GET['m'];?>&y=<?php echo $_GET['y'];?>">下一页&nbsp;<img src="images/right.png" /></a>
						<?php
						} else {
						?>
						<a href="<?php echo $_SERVER['PHP_SELF']; ?>?page=<?php echo ($result_f['num_page']+1); ?>">下一页&nbsp;<img src="images/right.png" /></a>
						<?php
						}
					}
				?>
			</div>
		</div>
		<!-- end 显示上一页下一页 -->
	</div>
	<?php include_once('sidebar.php'); ?> 
</div>
<?php include('footer.php');?> 
